package com.rchat.pocmini.activity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.rchat.pocmini.R;
import com.rchat.pocmini.account.PocAccount;
import com.rchat.pocmini.activity.addfriend.AddFriendPresenterImpl;
import com.rchat.pocmini.bean.SearchUserBean;
import com.rchat.pocmini.bean2eventbus.AddFriendBean;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by ljd on 2017/3/11.
 */


public class SearchUserResultAdapter extends BaseAdapter {

    private List<SearchUserBean.DataBean.UsersBean.UserBean> data;
    private LayoutInflater inflater;
    private Context context;

    public SearchUserResultAdapter(Context context, List<SearchUserBean.DataBean.UsersBean.UserBean> data) {
        this.data = data;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.search_user_result_list_view_item, null);
            holder = new ViewHolder();
            holder.ivAvatar = (ImageView) convertView.findViewById(R.id.iv_avatar);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvId = (TextView) convertView.findViewById(R.id.tv_id);
            holder.btnAddFriend = (Button) convertView.findViewById(R.id.btn_add_friend);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final SearchUserBean.DataBean.UsersBean.UserBean bean = data.get(position);
        holder.tvName.setText(bean.getAccount());
        holder.tvId.setText(bean.getDisplayName());
        if (bean.getUserName().equals(PocAccount.getPocAccountInstance(context).getUserId())) {
            holder.btnAddFriend.setText("自己");
            holder.btnAddFriend.setBackgroundColor(Color.parseColor("#dadbdd"));
            holder.btnAddFriend.setClickable(false);
        } else {
            holder.btnAddFriend.setText("加好友");
            holder.btnAddFriend.setBackgroundColor(Color.parseColor("#1fbaf3"));
            holder.btnAddFriend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.btnAddFriend.setText("已发送");
                    holder.btnAddFriend.setBackgroundColor(Color.parseColor("#dadbdd"));
                    holder.btnAddFriend.setClickable(false);
//                    //发送加好友请求
//                    String requestJson = createAddFriendJson(bean.getUserName());
//                    startSendAddFriendThread(requestJson);
//                    if (dialog != null) {
//                        dialog.dismiss();
//                    }
                    AddFriendBean b = new AddFriendBean();
                    b.setTarget(this.getClass(), AddFriendPresenterImpl.class,"onEventBusAddFriend");
                    b.setCurrentEvent(AddFriendBean.ADD_FRIEND);
                    b.setOtherName(bean.getUserName());
                    b.setUserName(AirtalkeeAccount.getInstance().getUserId());
                    EventBus.getDefault().post(b);
                }
            });
        }
        return convertView;
    }

    private static class ViewHolder {
        private Button btnAddFriend;
        private ImageView ivAvatar;
        private TextView tvName;
        private TextView tvId;
    }

}
